﻿Module PubVars
    Public ds As New DataSet("OR Simplex")
    Public nSteps As Integer = 0
    Public lInfeasible As Boolean = False
    Public Sub FixBlanks(ByRef dt As DataTable)
        For Each dr As DataRow In dt.Rows
            For Each dc As DataColumn In dt.Columns
                If dr(dc) Is Nothing OrElse IsDBNull(dr(dc)) OrElse dr(dc).ToString.Trim = "" Then
                    If dc.DataType = GetType(Decimal) Then
                        dr(dc) = 0.0
                    ElseIf dc.DataType = GetType(String) Then
                        dr(dc) = ""
                    Else
                        dr(dc) = 0
                    End If
                End If
            Next
        Next
    End Sub
    Public Sub ReorderColumns(ByRef dt As DataTable)
        Dim lstColumns As New List(Of String)
        For Each dc As DataColumn In dt.Columns
            lstColumns.Add(dc.ColumnName)
        Next
        Dim nOrdinal As Integer = 0
        If lstColumns.Contains("Basic") Then
            dt.Columns("Basic").SetOrdinal(nOrdinal)
            nOrdinal += 1
        End If
        Dim xCount As Integer = 0
        Dim sCount As Integer = 0
        Dim aCount As Integer = 0
        For Each Str As String In lstColumns
            If Str.Substring(0, 1) = "X" Then
                xCount += 1
            ElseIf Str.Substring(0, 1) = "S" Then
                sCount += 1
            ElseIf Str.Substring(0, 1) = "A" Then
                aCount += 1
            End If
        Next
        For x As Integer = 1 To xCount
            dt.Columns("X" & x.ToString).SetOrdinal(nOrdinal)
            nOrdinal += 1
        Next
        For s As Integer = 1 To sCount
            If dt.Columns.Contains("S" & s.ToString) Then
                dt.Columns("S" & s.ToString).SetOrdinal(nOrdinal)
                nOrdinal += 1
            End If
        Next
        For a As Integer = 1 To aCount
            If dt.Columns.Contains("A" & a.ToString) Then
                dt.Columns("A" & a.ToString).SetOrdinal(nOrdinal)
                nOrdinal += 1
            End If
        Next
        dt.Columns("RHS").SetOrdinal(dt.Columns.Count - 1)
        dt.Columns("Ratio").SetOrdinal(dt.Columns.Count - 1)
    End Sub
End Module
